﻿using System;
using System.Web;

namespace Game.Utilities.Session
{
    public class AccountSession
    {
        public static long AccountID
        {
            get
            {

               
                int userId = 0;
                if (HttpContext.Current != null && HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    userId = Convert.ToInt32(HttpContext.Current.User.Identity.Name.Split('|')[0]);
                }

                return userId;
            }
        }

        public static string AccessToken
        {
            get
            {
                var current = new SessionUtility().GetValue(SessionUtility.SESSION_Accesstoken);
                if (current == null) return string.Empty;
                return (current.ToString());
            }
            set
            {
                new SessionUtility().SetValue(SessionUtility.SESSION_Accesstoken, value);

            }
        }

        public static long AccountTotalBalance
        {
            get
            {
                var current = new SessionUtility().GetValue("AccountTotalBalance");
                if (current == null) return 0;
                return Convert.ToInt64(current);
            }
            set
            {
                new SessionUtility().SetValue("AccountTotalBalance", value);
            }
        }


        public static string AccountName
        {
            get
            {
                 
                string userName = string.Empty;

                if (HttpContext.Current != null && HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    userName = HttpContext.Current.User.Identity.Name.Split('|')[1];
                }

                return userName;
            }
        }

        public static string SessionName(long accountId, string username)
        {
            //accountId | username | ip
            return string.Format("{0}|{1}|{2}", accountId, username, HttpContext.Current.Request.UserHostAddress);
        }
    }

    public class SessionUtility
    {

        #region --- Khai báo tên các session ---

        //Dùng để lưu AccountID|AccountName|AccountTypeID|SessionKey|PasswordKey    //SessionKey|PasswordKey dùng để kiểm xoát đăng nhập 1 lần
        public const string SessionAccount = "Account";
        public const string SesionCurrentGameId = "CurrentGameSessionId";
        public const string SesionGameRoomID = "GameRoomId";
        public const string SessionFacebookID = "FacebookIDConfig";
        public const string SESSION_EMail = "Email";
        public const string SESSION_Books = "Books";
        public const string SESSION_Accesstoken = "Accesstoken";
        #endregion




        /// <summary>
        /// lấy ra giá trị session
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public object GetValue(string name)
        {
            return HttpContext.Current.Session[name];
        }




        /// <summary>
        /// hủy một session
        /// </summary>
        /// <param name="name"></param>
        public void Remove(string name)
        {
            HttpContext.Current.Session.Remove(name);

        }




        /// <summary>
        /// Nạp giá trị cho session
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public void SetValue(string name, object value)
        {
            HttpContext.Current.Session[name] = value;
        }

    }
}